//
//  ImageSliceOperation.m
//  iPictureSplitter
//
//  Created by Nikita Ivanyushchenko on 3/11/10.
//  Copyright 2010 MLS-Automatization. All rights reserved.
//

#import "ImageSliceOperation.h"


@implementation ImageSliceOperation

- (id) initWithImage:(NSImage *) anImage rect:(NSRect) aRect url:(NSURL*) anUrl
{
	self = [super init];  	
	if (self)
	{
		image	= [anImage retain];
		rect	= aRect;
		url		= [anUrl copy];
	}
	return self;	
}

-(void) main
{
	NSImage *target = [[NSImage alloc] initWithSize:NSMakeSize(rect.size.width,rect.size.height)];
	
	
	[target lockFocus];
	[image drawInRect:NSMakeRect(0,0,rect.size.width,rect.size.height) 
			 fromRect:rect
			operation:NSCompositeCopy
			 fraction:1.0];
	[target unlockFocus];
	
	
	NSData *imageData = [target  TIFFRepresentation];
	
	NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
	
	NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
	
	imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
		
	//write the data to file
	[imageData writeToURL: url atomically: NO];
	
	[target release];
}

-(void) dealloc
{
	[image release];
	[url release];
	[super dealloc];
}

@end
